package com.vn.toaa.tintinscreenoff;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.util.Log;

public class PhoneCallReceiver extends BroadcastReceiver {

	public static final String TAG = PhoneCallReceiver.class.getSimpleName();
	private static boolean sIsPhoneCallProcessing;

	@Override
	public void onReceive(Context context, Intent intent) {
		System.out.println("xxx: " + TAG);
		sIsPhoneCallProcessing = false;
		if (intent.getAction().equals("android.intent.action.PHONE_STATE")) {
			String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
			Log.d(TAG, "PhoneStateReceiver**Call State=" + state);

			if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
				Log.d(TAG, "PhoneStateReceiver**Idle");
			} else if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
				// Incoming call
				String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
				Log.d(TAG, "PhoneStateReceiver**Incoming call " + incomingNumber);
				sIsPhoneCallProcessing = true;
			} else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
				Log.d(TAG, "PhoneStateReceiver **Offhook");
				String outgoingNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
				Log.d(TAG, "PhoneStateReceiver **Outgoing call " + outgoingNumber);
				sIsPhoneCallProcessing = true;

			}
		} else if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) {
			// Outgoing call
			String outgoingNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
			Log.d(TAG, "PhoneStateReceiver **Outgoing call " + outgoingNumber);
			sIsPhoneCallProcessing = true;
		} else {
			Log.d(TAG, "PhoneStateReceiver **unexpected intent.action=" + intent.getAction());
		}
	}

	public static boolean isPhoneCallProcessing() {
		return sIsPhoneCallProcessing;
	}

	public static void setIsPhoneCallProcessing(boolean isCallProcessing) {
		PhoneCallReceiver.sIsPhoneCallProcessing = isCallProcessing;
	}
}
